// $_FILEHEADER_BEGIN ***************************
// 版权声明:Timer
// Copyright © 2015 - Timer All Rights Reserved
// 文件名称: ITLoopView.m
// 创建日期: 15/7/27
// 创 建 人: 甄鑫
// 文件说明: <#content#>
// $_FILEHEADER_END ******************************


#import "ITLoopView.h"
#import "UIView+UIView_ITAdditions.h"

@implementation ITLoopView
{
    UIScrollView    *_mpMainScroller;
    UIView          *_mpLeftView;
    UIView          *_mpRightView;
    UIView          *_mpMiddleView;
    __weak id       _mpDelegate;
    CGSize          _msPageSize;
    CGFloat         _mfBeginX;
    CGFloat         _mfEndX;
}

- (void)setLoopViewDelegate:(id)delegate
{
    _mpDelegate = delegate;
}

- (void)startSetViewWithArr:(NSArray *)subViewArr
{
    if (subViewArr.count != 3)
    {
        return;
    }
    [self startSetViewWithLeft:[subViewArr firstObject]
                    middleView:[subViewArr objectAtIndex:1]
                     rightView:[subViewArr lastObject]];
}

- (void)startSetViewWithLeft:(UIView *)leftView
                  middleView:(UIView *)middelView
                   rightView:(UIView *)rightView
{
    _msPageSize = leftView.frame.size;
    [self initViewWithSize:leftView.frame.size];
    [_mpLeftView addSubview:leftView];
    [_mpMiddleView addSubview:middelView];
    [_mpRightView addSubview:rightView];
}
/**
 *  总共利用 3 屏,三屏数据
 */
- (void)initViewWithSize:(CGSize)pageViewSize
{
    if (!_mpMainScroller)
    {
        _mpMainScroller = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, pageViewSize.width, pageViewSize.height)];
        _mfBeginX = pageViewSize.width;
        _mfEndX = pageViewSize.width * 4;
        [_mpMainScroller setDelegate:self];
        [_mpMainScroller setPagingEnabled:YES];
        [_mpMainScroller setContentSize:CGSizeMake(pageViewSize.width * 3, pageViewSize.height)];
    
        [_mpMainScroller setShowsVerticalScrollIndicator:NO];
        [_mpMainScroller setShowsHorizontalScrollIndicator:NO];
        
        [_mpMainScroller setContentOffset:CGPointMake(pageViewSize.width, 0)];
        [self addSubview:_mpMainScroller];
        [self setFrame:_mpMainScroller.frame];
        
        _mpLeftView = [self createViewWithFrame:CGRectMake(0, 0, pageViewSize.width, pageViewSize.height)];
        _mpMiddleView = [self createViewWithFrame:CGRectMake(pageViewSize.width * 1, 0, pageViewSize.width, pageViewSize.height)];

        _mpRightView = [self createViewWithFrame:CGRectMake(pageViewSize.width * 2, 0, pageViewSize.width, pageViewSize.height)];
    }
}

- (UIView *)createViewWithFrame:(CGRect)frame
{
    UIView * view = [[UIView alloc]initWithFrame:frame];
    [view setBackgroundColor: [UIColor clearColor]];
    [_mpMainScroller addSubview:view];
    return view;
}

- (void)resetScroller:(UIScrollView *)scrollView
{
    [scrollView setContentOffset:CGPointMake(_msPageSize.width, 0)];
}

- (void)toLeftOverLoop
{
    
    [_mpLeftView setFrame_x:_msPageSize.width];
    [_mpMiddleView setFrame_x:_msPageSize.width * 2];
    [_mpRightView setFrame_x:0];
    
    UIView * tmpView = _mpRightView;
    _mpRightView = _mpMiddleView;
    _mpMiddleView = _mpLeftView;
    _mpLeftView = tmpView;
    
    [self notiToViewLoopType:TYPE_LOOP_VIEW_LEFT];
}

- (void)toRightOverLoop
{
    [_mpLeftView setFrame_x:_msPageSize.width * 2];
    [_mpMiddleView setFrame_x:0];
    [_mpRightView setFrame_x:_msPageSize.width];
    
    UIView * tmpView = _mpLeftView;
    _mpLeftView = _mpMiddleView;
    _mpMiddleView = _mpRightView;
    _mpRightView = tmpView;
    [self notiToViewLoopType:TYPE_LOOP_VIEW_RIGHT];
}

- (void)notiToViewLoopType:(TYPE_LOOP_VIEW)loopType
{
    if ([_mpDelegate respondsToSelector:@selector(curSelectLeft:middleView:rightView:loopType:)])
    {
        [_mpDelegate curSelectLeft:[_mpLeftView.subviews firstObject]
                        middleView:[_mpMiddleView.subviews firstObject]
                         rightView:[_mpRightView.subviews firstObject]
                          loopType:loopType];
    }
}

- (NSArray *)getAllSubView
{
    return @[[_mpLeftView.subviews firstObject],
            [_mpMiddleView.subviews firstObject],
            [_mpRightView.subviews firstObject],];
}


- (void)setLoopViewHeight:(CGFloat)height
{
    [self setFrame_height:height];
    [_mpLeftView setFrame_height:height];
    [_mpMiddleView setFrame_height:height];
    [_mpRightView setFrame_height:height];
    [_mpMainScroller setFrame_height:height];
}

- (void)activeToLeft
{
    [self resetScroller:_mpMainScroller];
    [self toLeftOverLoop];
}

- (void)activeToRight
{
    [self resetScroller:_mpMainScroller];
    [_mpLeftView setFrame_x:_msPageSize.width * 2];

    [UIView animateWithDuration:0.5 animations:^{
        [_mpMiddleView setFrame_x:0];
        [_mpRightView setFrame_x:_msPageSize.width];
    }];

    UIView * tmpView = _mpLeftView;
    _mpLeftView = _mpMiddleView;
    _mpMiddleView = _mpRightView;
    _mpRightView = tmpView;
    
    [self notiToViewLoopType:TYPE_LOOP_VIEW_RIGHT];

}

#pragma mark - **************** UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x >= _msPageSize.width * 2)
    {
        [self resetScroller:scrollView];
        [self toRightOverLoop];
    }
    else if (scrollView.contentOffset.x <= 0)
    {
        [self resetScroller:scrollView];
        [self toLeftOverLoop];
    }
}

@end

#define kLoadNibWithClass(pClass) [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass(pClass) owner:nil options:nil]firstObject];
@implementation ITLoopAutoCreateView
{
    ITLoopView * _lootView;
    NSArray * _dataArr;
    NSInteger _curIndex;
    
    UIView <ITLoopItemViewProtocol> * _leftView;
    UIView <ITLoopItemViewProtocol> * _middleView;
    UIView <ITLoopItemViewProtocol> * _rightView;
    
}

- (void)setLoopItemXibViewClass:(Class)pClass itemViewFrame:(CGRect)itemViewFrame
{
    _leftView   = kLoadNibWithClass(pClass);
    _middleView = kLoadNibWithClass(pClass);
    _rightView  = kLoadNibWithClass(pClass);
    
    [self initLootViewWithItemFrame:itemViewFrame];
}

- (void)setLoopItemViewClass:(Class)pClass itemViewFrame:(CGRect)itemViewFrame
{
    _leftView   = [[pClass alloc]init];
    _middleView = [[pClass alloc]init];
    _rightView  = [[pClass alloc]init];
    
    [self initLootViewWithItemFrame:itemViewFrame];
}

- (void)initLootViewWithItemFrame:(CGRect)frame
{
    [_leftView setFrame:frame];
    [_middleView setFrame:frame];
    [_rightView setFrame:frame];
    
    _lootView = [[ITLoopView alloc]init];
    [_lootView startSetViewWithLeft:_leftView middleView:_middleView rightView:_rightView];
    [_lootView setLoopViewDelegate:self];
    [self addSubview:_lootView];
}

- (void)setLoopViewDataArr:(NSArray *)viewArr
{
    _dataArr = viewArr;
    [self reloadData];
}

- (void)setLoopViewFrame:(CGRect)loopViewFrame
{
    [_lootView setFrame:loopViewFrame];
}

// 1 ---> _dataArr.count
- (NSInteger)getCurIndex
{
    return _curIndex;
}

- (ITLoopView *)getLoopView
{
    return _lootView;
}

- (void)getCurIndexCB:(void(^)(NSInteger curIndex))cb
{
    
}

- (void)setDefaultIndexWithIndex:(NSInteger)index
{
    _curIndex = index;
}

- (void)curSelectLeft:(UIView *)leftView
           middleView:(UIView *)middelView
            rightView:(UIView *)rightView
             loopType:(TYPE_LOOP_VIEW)loopType
{
    if (loopType == TYPE_LOOP_VIEW_LEFT)
    {
        [self toLeftOverLoop];
        _curIndex --;
        if (_curIndex < 1)
        {
            _curIndex = _dataArr.count;
        }
        [_leftView setLoopItemViewWithData:[self getCurViewData:TYPE_LOOP_VIEW_LEFT]];
    }
    else if (loopType == TYPE_LOOP_VIEW_RIGHT)
    {
        [self toRightOverLoop];
        _curIndex ++;
        if (_curIndex > _dataArr.count)
        {
            _curIndex = 1;
        }
        [_rightView  setLoopItemViewWithData:[self getCurViewData:TYPE_LOOP_VIEW_RIGHT]];
    }
    
    if (self.curIndexCB)
    {
        self.curIndexCB(self);
    }
}

- (void)reloadData
{
    [_leftView   setLoopItemViewWithData:[self getCurViewData:TYPE_LOOP_VIEW_LEFT]];
    [_middleView setLoopItemViewWithData:[self getCurViewData:TYPE_LOOP_VIEW_MIDDLE]];
    [_rightView  setLoopItemViewWithData:[self getCurViewData:TYPE_LOOP_VIEW_RIGHT]];
}

- (id)getCurViewData:(TYPE_LOOP_VIEW)lootType
{
    NSInteger dataIndex = (_curIndex - 1);
    
    if(_dataArr.count == 0)
    {
        ITLog(@"数据为空");
        return nil;
    }
    
    switch (lootType) {
        case TYPE_LOOP_VIEW_LEFT:
            dataIndex --;
            break;
        case TYPE_LOOP_VIEW_MIDDLE:
            break;
        case TYPE_LOOP_VIEW_RIGHT:
            dataIndex ++;
            break;
        default:
            break;
    }
    
    if (dataIndex < 0)
    {
        dataIndex = _dataArr.count - 1;
    }
    else if (dataIndex >= _dataArr.count)
    {
        dataIndex = 0;
    }
    
    ITLogSP(@"%d--%d--%@",lootType,dataIndex,[_dataArr objectAtIndex:dataIndex]);
    
    return [_dataArr objectAtIndex:dataIndex];
}

- (void)toLeftOverLoop
{
    UIView * tmpView = _rightView;
    _rightView = _middleView;
    _middleView = _leftView;
    _leftView = tmpView;
}

- (void)toRightOverLoop
{
    UIView * tmpView = _leftView;
    _leftView = _middleView;
    _middleView = _rightView;
    _rightView = tmpView;
}

@end



